package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.BaseContext;
import com.example.common.R;
import com.example.domain.ShoppingCart;
import com.example.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {

        Long userId = BaseContext.getThreadLocal();

        shoppingCart.setUserId(userId);

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<ShoppingCart>();
        wrapper.eq(ShoppingCart::getUserId, userId);

        if (dishId != null) {

            wrapper.eq(ShoppingCart::getDishId, dishId);

        } else {

            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        }

        ShoppingCart one = shoppingCartService.getOne(wrapper);

        if (one != null) {
            int number = one.getNumber() + 1;
            one.setNumber(number);
            shoppingCartService.updateById(one);
        } else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        redisTemplate.delete("shoppingCart");

        return R.success(one);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {

        List<ShoppingCart> shoppingCart = (List<ShoppingCart>) redisTemplate.opsForValue().get("shoppingCart");

        if (shoppingCart !=null) {
            return R.success(shoppingCart);
        }

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<ShoppingCart>();

        Long userId = BaseContext.getThreadLocal();

        wrapper.eq(ShoppingCart::getUserId, userId);

        wrapper.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(wrapper);

        redisTemplate.opsForValue().set("shoppingCart", list, 3600L, TimeUnit.SECONDS);

        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R<String> clean() {

        Long userId = BaseContext.getThreadLocal();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<ShoppingCart>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(wrapper);

        redisTemplate.delete("shoppingCart");

        return R.success("清除购物车成功！");

    }

    @PostMapping("/sub")
    public R<String> delete(@RequestBody ShoppingCart shoppingCart) {

        Long userId = BaseContext.getThreadLocal();

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<ShoppingCart>();
        wrapper.eq(ShoppingCart::getUserId, userId);

        ShoppingCart one;
        if (dishId != null) {

            wrapper.eq(ShoppingCart::getDishId, dishId);
            one =shoppingCartService.getOne(wrapper);
            if (one.getNumber() == 1) {
                shoppingCartService.removeById(one.getId());
            } else {
                int number = one.getNumber() - 1;
                one.setNumber(number);
                shoppingCartService.updateById(one);
            }

        } else {
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            one = shoppingCartService.getOne(wrapper);
            if (one.getNumber() == 1) {
                shoppingCartService.removeById(one.getId());
            } else {
                int number = one.getNumber() - 1;
                one.setNumber(number);
                shoppingCartService.updateById(one);
            }

        }

        redisTemplate.delete("shoppingCart");

        return R.success("删除成功！");
    }

}
